<?php

namespace app\common\model\ad;

use app\common\model\BaseModel;
use app\shop\model\supplier\Supplier as SupplierModel;

/**
 * 广告模型
 */
class Ad extends BaseModel
{
    protected $name = 'ad';
    protected $pk = 'ad_id';

    /**
     * 获取列表
     */
    public function getList($limit = 20,$shop_supplier_id=0,$where=[])
    {

        $map = [];
//        $map[] = ['is_delete', '=', 0];
//        $map[] = ['is_audit', '=', 1];
        $where2 = [];

        if (isset($limit['province_id']) && $limit['province_id']) {
            $where2[] = ['province_id', '=', $limit['province_id']];
        }
        if (isset($limit['city_id']) && $limit['city_id']) {
            $where2[] = ['city_id', '=', $limit['city_id']];
        }
        if (isset($limit['region_id']) && $limit['region_id']) {
            $where2[] = ['region_id', '=', $limit['region_id']];
        }


        if (isset($limit['search']) && $limit['search']) {
            $map[] = ['name', 'like', '%' . trim($limit['search']) . '%'];
        }

        $shop_supplier_ids = [];
        $map2 = [];
        if($shop_supplier_id){
            $shop_supplier_ids[] = $shop_supplier_id;
            $map2[] = ['shop_supplier_id','in',$shop_supplier_ids];
        }

        if($map){
            $shop_supplier_ids = (new SupplierModel())->where($map) -> column('shop_supplier_id');
            $map2[] = ['shop_supplier_id','in',$shop_supplier_ids];
        }


        return $this->with(['image','category']) -> field('*,type type_text')->where($map2)->where($where2)->order(['audit_status'=> 'asc','sort' => 'asc'])
            ->paginate($limit);
    }

    /**
     * 广告详情
     */
    public static function detail($ad_id)
    {
        return (new static())->with(['image'])->find($ad_id);
    }
    /**
     * 获取列表
     */
    public function getLists($where,$limit = 20)
    {
         // 获取列表
        $data = $this->limit($limit)
                        ->where($where)
                        ->with(['image'])
                        ->order('sort asc')
                        ->select();

        // 隐藏api属性
        !$data->isEmpty() ;
        return $data;
    }
	 /**
     * 关联图片
     */
    public function image()
    {
        return $this->belongsTo('app\common\model\file\UploadFile', 'image_id', 'file_id');
    }
     /**
     * 关联分类表
     * @return \think\model\relation\BelongsTo
     */
    public function category()
    {
        $module = self::getCalledModule() ?: 'common';
        return $this->BelongsTo("app\\{$module}\\model\\ad\\AdCategory", 'category_id', 'category_id');
    }
  
}